<template>
  <div class="relative main-container">
    <!-- 顶部背景 -->
    <img src="@/assets/bg2.jpg" class="w-full h-[333px]" />
    <!-- 主体内容 -->
    <div class="absolute top-5 w-full px-[15px]">
      <div class="flex items-center">
        <p class="text-lg font-bold mb-0">
          {{ userStore.userInfo.fullName }}
        </p>
        <span
          class="flex items-center justify-center ml-2"
          style="width: 20px; height: 20px; background: rgb(255 255 255 / 45%); border-radius: 50%"
          @click="showBottom = true"
        >
          <van-icon name="play" style="font-size: 12px; transform: rotate(90deg); color: #a6a6a6; left: -1px" />
        </span>
      </div>

      <p class="text-xs font-normal text-color-[#646A73] mt-1 text-[#646A73]">
        {{ userStore.userInfo.email }}
      </p>
      <!-- 切换语言 -->
      <div class="flex justify-between px-5 py-3 bg-white rounded-md mt-8" @click="toggleLanguage">
        <span>語言 / Language</span>
        <span class="text-[#A6A6A6]">{{ $t('languageNow') }}</span>
      </div>
      <!-- 按钮 -->
      <div class="space-y-[21px] px-5 mt-[168px]">
        <van-button type="primary" block class="btn-submit" color="white" @click="handleLogout">
          {{ $t('logout') }}
        </van-button>
      </div>

      <!-- 底部弹出 -->
      <van-popup v-model:show="showBottom" position="bottom" :style="{ height: '30%' }">
        <div class="px-4 py-6 space-y-6">
          <div
            v-for="item in accounts"
            :key="item.id"
            class="flex items-center justify-between"
            @click="selectAccount(item)"
          >
            <div class="flex items-center space-x-4">
              <div>
                <div class="text-lg font-semibold">{{ item.name }}</div>
                <div class="text-sm text-gray-400">{{ item.email }}</div>
              </div>
            </div>
            <div>
              <div
                class="w-6 h-6 rounded-full flex items-center justify-center"
                :class="item.id === currentAccountId ? 'bg-[#e6e6e6]' : 'border border-gray-500'"
              >
                <template v-if="item.id === currentAccountId">
                  <svg class="w-4 h-4" fill="currentColor" viewBox="0 0 24 24">
                    <path d="M20.285 6.709l-11.403 11.403-5.657-5.657 1.414-1.414 4.243 4.243 9.99-9.99z" />
                  </svg>
                </template>
              </div>
            </div>
          </div>

          <!-- 添加新账号 -->
          <div class="flex items-center space-x-4 pt-2" @click="addAccount">
            <div class="w-14 h-14 text-white rounded-full bg-[#e6e6e6] text-4xl flex items-center justify-center">
              +
            </div>
            <div class="text-lg font-semibold">添加账号</div>
          </div>
        </div>
      </van-popup>
    </div>
  </div>
</template>

<script setup>
import { useRouter } from 'vue-router';
import { useUserStore } from '@/store/modules/user-info';
import { showToast } from 'vant';
import { useI18n } from 'vue-i18n';
import { onMounted, ref, reactive, computed } from 'vue';
import { useMultiAccountStore } from '@/store/modules/multi-account';

onMounted(() => {
  document.body.style.backgroundColor = '#f7f8f9'; // 当前页面背景色
});

const { t } = useI18n();

const router = useRouter();
const userStore = useUserStore();
const multiAccountStore = useMultiAccountStore();

const showBottom = ref(false); // 是否显示下方弹窗
const accounts = computed(() => multiAccountStore.accounts);
const currentAccountId = computed(() => multiAccountStore.currentAccountId);

function selectAccount(account) {
  multiAccountStore.activateAccount(account.id);
  showBottom.value = false;
  showToast(`已切换到 ${account.name}`);
}

function addAccount() {
  router.push({
    name: 'Login',
    query: {
      from: 'account-switch',
    },
  });
}

// 切换语言
const toggleLanguage = () => {
  const value = userStore.i18nLocale === 'zh' ? 'en' : 'zh';
  userStore.setLocal(value);
};

const handleLogout = async () => {
  await multiAccountStore.removeAccount(userStore.userInfo.userId);
  await userStore.LogOut({});
  showToast(t('showSuccessToast'));
  router.push('/login');
};
</script>

<style lang="less" scoped>
:root {
  --van-field-input-text-color: #333;
  --van-button-primary-background-color: #ff5722;
}

.main-container {
  background-color: var(--color-background-2);
}

.welcome__text {
  /** 文本1 */
  font-size: 22px;
  font-weight: 400;
  letter-spacing: 1px;
  line-height: 30.18px;
  color: rgba(100, 106, 115, 1);
  text-align: left;
  vertical-align: top;
}

.login-input {
  border-radius: 6px;
  background: rgba(255, 255, 255, 1);
  border: 1.5px solid rgba(208, 211, 213, 1);
  box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.1);
}

.btn-submit {
  border-radius: 6px;
  margin-top: 31px !important;
  height: 53.5px;
  font-size: 14px;
  font-weight: 400;
  letter-spacing: 0px;
  line-height: 24.7px;
  color: black !important;
  text-align: left;
  vertical-align: top;
}
</style>
